//
//  BaseViewControllerWithHeader.swift
//  suda
//
//  Created by youai on 2019/7/19.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit

//有标题栏和返回按钮的viewcontroller
class BaseViewControllerWithHeader : BaseViewController {
    
    let headView = UIViewWithGradientLayer()
    let backButton = Button()
    let headTitle = UILabel()
    var lastRightButton : UIView? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        safeView.addSubview(headView)
        headView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(safeView.snp.top).offset(64-20)
            make.top.equalToSuperview().offset(-44)
        }
//        headView.drawGradientLayer(color1: .x29B563, color2: .x17BA7C, x1: 0.5, y1: 1, x2: 0, y2: 0)
        headView.backgroundColor = .clear
        
        
        backButton.setImage(UIImage(named: "L_Back") , for: .normal)
        headView.addSubview(backButton)
        backButton.snp.makeConstraints { make in
//        let backButton = headView.drawButton(imageNamed: "btn_back_white") { (make) in
            make.centerX.equalTo(headView.snp.left).offset(5+33*0.5)
            make.centerY.equalTo(headView.snp.bottom).offset(-6-30*0.5)
        }
        backButton.imageEdgeInsets = .zero
        backButton.hitEdgInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
        backButton.addTarget(self, action: #selector(_onBackButtonClick), for: .touchUpInside)
        
        headView.drawLabel(label:headTitle, text: headTitle.text ?? "", fontSize: 16,fontWeight: .medium, textColor: .x333333) { (make) in
//            make.width.equalTo(180)
            make.left.greaterThanOrEqualTo(backButton.snp.right).offset(10)
            make.centerX.equalToSuperview()
            make.centerY.equalTo(headView.snp.bottom).offset(-13-16*0.5)
        }
        headTitle.textAlignment = .center
        headTitle.minimumScaleFactor = 0.5
        headTitle.adjustsFontSizeToFitWidth = true
    }
    
    //懒加载内部自动完成会失效，所以放在外面
    func _drawScrollView()->ConstraintsScrollView{
        let s = ConstraintsScrollView()
        safeView.addSubview(s)
        s.snp.makeConstraints({ (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(headView.snp.bottom)
        })
        return s
    }
    
    ///标题栏下方，自适应内容的scrollView（懒加载）
    lazy var scrollView : ConstraintsScrollView = {
        return _drawScrollView()
    }()
    
    ///搜索栏对象（懒加载）
    lazy var headerSearchBar : HeaderSearchBar = {
       let view = HeaderSearchBar()
        view.backgroundColor = .white
        view.setParent(vc: self)
        return view
    }()
    
    ///VC弹出
    @objc func _onBackButtonClick(){
        scrollView.clipsToBounds = true
        if let nav = self.navigationController ?? self.tabBarController?.navigationController {
            nav.popViewController(animated: true)
        }
        else {
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    ///绘制标题栏右侧的按钮，从右至左
    func drawRightButton(imageName:String,button:Button? = nil)->Button{
        let button = headView.drawButton(button:button, imageNamed: imageName) { (make) in
            make.centerY.equalTo(headView.snp.bottom).offset(-9-21*0.5)
            if lastRightButton == nil {
                make.centerX.equalTo(headView.snp.right).offset(-25-21*0.5)
            }else{
                make.centerX.equalTo(lastRightButton!).offset(-21-20)
            }
            make.width.equalTo(21+9+9)
            make.height.equalTo(21+9+9)
        }
        lastRightButton = button
        return button
    }
    
    ///绘制标题栏右侧的文字按钮
    func drawRightButton(title:String,button:Button? = nil)->Button{
        let button = headView.drawButton(title: title, titleColor: .x333333, fontSize: 14,fontWeight:.medium) { (make) in
            make.centerY.equalTo(headView.snp.bottom).offset(-9-21*0.5)
            if lastRightButton == nil {
                make.right.equalTo(headView.snp.right).offset(-20)
            }else{
                make.right.equalTo(lastRightButton!.snp.left).offset(-10)
            }
        }
        button.hitEdgInsets = .init(top: -10, left: -10, bottom: -10, right: -10)
        lastRightButton = button
        return button
    }
}

